package com.g7.figmaapi.common.aop;

import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
 * AOP实现事务管理
 * @author taoxiangqian
 * @since 2023/10/26
 **/
@Aspect
@Component
public class TransactionManageAop {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.g7.*.service..*.*(..))";

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean
    public TransactionInterceptor txAdvice() {
        DefaultTransactionAttribute attrRequired = new DefaultTransactionAttribute();
        attrRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        DefaultTransactionAttribute attrRequiredReadOnly = new DefaultTransactionAttribute();
        attrRequiredReadOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        attrRequiredReadOnly.setReadOnly(true);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        source.addTransactionalMethod("save*", attrRequired);
        source.addTransactionalMethod("add*", attrRequired);
        source.addTransactionalMethod("insert*", attrRequired);
        source.addTransactionalMethod("delete*", attrRequired);
        source.addTransactionalMethod("del*", attrRequired);
        source.addTransactionalMethod("update*", attrRequired);
        source.addTransactionalMethod("upd*", attrRequired);
        source.addTransactionalMethod("exec*", attrRequired);
        source.addTransactionalMethod("set*", attrRequired);
        source.addTransactionalMethod("get*", attrRequiredReadOnly);
        source.addTransactionalMethod("sel*", attrRequiredReadOnly);
        source.addTransactionalMethod("select*", attrRequiredReadOnly);
        source.addTransactionalMethod("query*", attrRequiredReadOnly);
        source.addTransactionalMethod("find*", attrRequiredReadOnly);
        source.addTransactionalMethod("list*", attrRequiredReadOnly);
        source.addTransactionalMethod("count*", attrRequiredReadOnly);
        source.addTransactionalMethod("is*", attrRequiredReadOnly);
        return new TransactionInterceptor(transactionManager, source);
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
}
